package com.remote;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.io.*;
import java.net.*;
import android.os.*;

import android.os.Bundle;

import android.util.Log;

import java.util.Enumeration;

public class Utils{
    
    public static MessageDigest md;

    public static void initMD5(){
        try{
            md = MessageDigest.getInstance("MD5");
        }catch(NoSuchAlgorithmException e){
            Log.e("md5Utils","NoSuchAlgorithmException: "+e.toString());
        }
    }
    
    public static String getPacketData(String data){
        return getMD5(data)+"|"+data;
    }
    
    public static String getMD5(String data){
        if(md == null) initMD5();
        byte[] tmp = md.digest(data.getBytes());
        md.reset();
        return convertToHex(tmp);
    }
    
    public static boolean check_data(String checksum,String data){
            String md5_tmp = getMD5(data);
            
            Log.i("md5Utils",checksum);
            Log.i("md5Utils",md5_tmp);
            
            if(checksum.compareTo(md5_tmp)==0) return true;
            else return false;
    }
    
    private static String convertToHex(byte[] data) { 
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) { 
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do { 
                if ((0 <= halfbyte) && (halfbyte <= 9)) 
                    buf.append((char) ('0' + halfbyte));
                else 
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        } 
        return buf.toString();
    }
    
    //! returns local ip address ... hopefuly Wifi address    
    public static String getLocalIpAddress(){
        try{
            for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
                NetworkInterface intf = en.nextElement();
                for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();enumIpAddr.hasMoreElements();){
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if(!inetAddress.isLoopbackAddress()){
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }catch(SocketException e){
            Log.e("getLocalIpAddress",e.toString());
        }
        return null;
    }
    
    public static String[] delFirstElement(String[] data){
        String[] tmp = new String[data.length-1];
        System.arraycopy(data,1,tmp,0,data.length-1);
        return tmp;
    }
    
    public static Bundle getDataBundle(String[] data){
        Bundle tmp = new Bundle();
        tmp.putStringArray("data",data);
        return tmp;
    }
    
    public static Bundle getStringBundle(String data){
		Bundle tmp = new Bundle();
		tmp.putString("data",data);
		return tmp;
	}

    public static Bundle getServerBundle(String[] data){
        Bundle tmp = new Bundle();
        tmp.putString("name",data[0]);
        tmp.putString("address",data[1]);
        tmp.putInt("port",Integer.parseInt(data[2]));
        tmp.putString("info",data[3]);
        return tmp;
    }
}
